package Pdfbox;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.IOException;

public class PDFBoxExtractAllText {

    public static String extractAllText(String filePath) throws IOException {
        File file = new File(filePath);
        PDDocument document = PDDocument.load(file);

        // 使用 PDFTextStripper 提取文本
        PDFTextStripper textStripper = new PDFTextStripper();
        String text = textStripper.getText(document);

        document.close();
        return text;
    }

    public static void main(String[] args) {
        // 禁用字体缓存
        System.setProperty("pdfbox.fontcache", "");

        try {
            String filePath = "D:\\test.pdf";
            String allText = extractAllText(filePath);
            System.out.println("Extracted Text:\n" + allText);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}